﻿using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace jiaobenPy
{
    public class Runner
    {
        internal ScriptEngine engine { get; set; }
        internal ScriptScope scope { get; set; }
        public Runner()
        {
            engine = Python.CreateEngine();
            scope = engine.CreateScope();

            Import("os", "sys");
            Exec($"sys.path.append(r\"{Path.GetDirectoryName(Assembly.GetAssembly(typeof(Runner)).Location).Replace("\\", "/")}/lib/\")");
        }

        public void Import(params string[] names)
        {
            foreach (var name in names) engine.Execute($"import {name}\n", scope);
        }

        public dynamic Exec(string command)
        {
            var rlt = engine.Execute($"{command}\n", scope);
            return rlt;
        }

        public dynamic ReadValue(string name)
        {
            return scope.GetVariable(name);
        }

        public string ReadVersion()
        {
            Exec("py_version = sys.version");
            var version = ReadValue("py_version");
            return version;
        }
    }
}
